<?php include("header.php"); ?>
<style type="text/css">
td {
	width: 50%;
}
</style>
<?php
function makePin($lenth =5) { 
    // makes a random alpha numeric string of a given lenth 
    $aZ09 = array_merge(range('A', 'Z'), range('a', 'z'),range(0, 9)); 
    $out =''; 
    for($c=0;$c < $lenth;$c++) { 
       $out .= $aZ09[mt_rand(0,count($aZ09)-1)]; 
    } 
    return $out; 
} 
?>
<script>
$(document).ready(function() {
	$("#step2, #step3, #val, #installing, #install").hide();
	$("#substep1").click(function() { // Hijack click of next step (step1 to step2)
		var host = $("input[name=host]").val();
		var dbname = $("input[name=dbName]").val();
		var dbuser = $("input[name=dbUser]").val();
		var dbpass = $("input[name=dbPass]").val();
		// Check if can connect to database before starting step 2
		$("#val").load("ajaxStep1.php", { dbName: dbname, dbUser: dbuser, dbname: dbname, host: host, dbPass: dbpass }, function() {
			if ($("#val").html() == "no") {
			$("#step1").fadeOut(500); // Fadeout step1
			$.doTimeout(500, function() {
			$("#step2").fadeIn(); // Fadein step2 after 0.5 seconds , to prevent overlapping
			});
			} else {
				alert("Could not connect!");
			}
		});
	});
	$("#backToStep1").click(function() { // Hijack back link from step 2 to step 1
		$("#step2").fadeOut(500); // Fade out step 2
		$.doTimeout(500, function() {
			$("#step1").fadeIn(); //Fadein step1 after 0.5 seconds , to prevent overlapping
		});
	});
	$("#substep2").click(function() { // Hijack link from step2 to step3
		$("#step2").fadeOut() // Fade out step 2
		$.doTimeout(500, function() {
			$("#step3").fadeIn(500) // Fadein step3 after 0.5 seconds , to prevent overlapping
		});
	});
	$("#backToStep2").click(function() { // Hijack back link from step 3 to step 2
		$("#step3").fadeOut(500); // Fade out step 3
		$.doTimeout(500, function() {
			$("#step2").fadeIn(500) // Fadein step2 after 0.5 seconds , to prevent overlapping
		});
	});
	$("input[name=install!]").click(function() {
		var host = $("input[name=host]").val();
		var dbname = $("input[name=dbName]").val();
		var dbuser = $("input[name=dbUser]").val();
		var dbpass = $("input[name=dbPass]").val();
		var name = $("input[name=name]").val();
		var username = $("input[name=username]").val();
		var password = $("input[name=password]").val();
		var domain = $("input[name=domain]").val();
		$("#backToStep2").fadeOut(500);
		$("#installing").fadeIn(500);
		$("#install").load("install.php", { 
			host: host,
			dbname: dbname,
			dbuser: dbuser,
			dbpass: dbpass,
			name: name,
			username: username,
			password: password,
			domain: domain
		}, function() {
			$("#installing").fadeOut(500);
			$.doTimeout(500, function() {
				$("#installing").html("3txt has been installed! Click <a href='../'>here</a> to go to "+name+"!");
				$("#installing").fadeIn(500);
			});
		});		
	});
});
</script>
<div id="step1">
<div id="content" class="wrap">
<h2>Step 1</h2>
<p>
	This is where we establish connection to your mysql database. If you don't know what host is, just leave it
</p>
<table>
<tr><td>Host</td><td><input type="text" name="host" value="localhost" /></td></tr>
<tr><td>Database Name</td><td><input type="text" name="dbName"  /></td></tr>
<tr><td>Database Username</td><td><input type="text" name="dbUser" /></td></tr>
<tr><td>Database Password</td><td><input type="text" name="dbPass" /></td></tr>
<tr><td></td><td><input type="submit" value="Next Step!" name="submit" id="substep1" /></td></tr>
</table>
</div>
</div>
<div id="val"></div>
<div id="step2">
<div class="wrap" id="content">
<h2>Step 2</h2>
<p>Service Information. Here you can choose things such as username and password, and service name. Also the location where 3txt is located. If it is http://yourdomain.com, leave it as it is, if it it is located at something like http://yourdomain.com/3txt/, add the /3txt, make sure there is no "/" at the end though.</p>
<table>
<tr><td>Name</td><td><input type="text" name="name" value="3txt" /></td></tr>
<tr><td>Username</td><td><input type="text" name="username" value="admin" /></td></tr>
<tr><td>Password</td><td><input type="text" name="password" value="<?php echo makePin(); ?>" /></td></tr>
<tr><td>Location</td><td><input type="text" name="domain" value="http://<?php echo $_SERVER['SERVER_NAME']; ?>" /></td></tr>
<tr><td><input type="submit" value="Back" name="submit" id="backToStep1" /></td><td><input type="submit" name="submitStep2" id="substep2" value="Next Step!" /></td></tr>
</table>
</div>
</div>
<div id="step3">
<div class="wrap" id="content">
<h2>Step 3</h2>
<p>Click install to run the install!</p>
<table>
<tr><td><input type="submit" value="Back" id="backToStep2" /></td><td><input type="submit" value="Install!" name="install!"  /></td></tr>
</table>
</div>
</div>
<div class="wrap" id="installing">Installing...</div>
<div id="install"></div>
</body>
</html>